﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.UserProfileManagement
{
    public class UserProfileCreationBLL
    {
        public static List<Roles> GetAllRoles()
        {
            List<Roles> foundElements = null;
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foundElements = (from roles in ctx.Roles
                                 orderby roles.Role
                                 select roles).ToList();
            }
            return foundElements;
        }

        public static VincularPerfiles AddNewProfile(string profileName, List<Roles> roleCollection, bool isSiteContributor)
        {
            VincularPerfiles newElement = null;

            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                newElement = new VincularPerfiles { Title = string.Empty, Perfil = profileName.ToUpper(), ContribuyenteEnSitio= isSiteContributor };
                foreach (Roles currentRole in roleCollection)
                {
                    ctx.Roles.Attach(currentRole);
                    newElement.TipoRole.Add(currentRole);
                }
                ctx.Perfiles.InsertOnSubmit(newElement);
                ctx.SubmitChanges();
            }

            return newElement;
        }

        public static bool ProfileExists(string profileName)
        {
            bool exists = false;
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                exists = ctx.Perfiles.Count(vp => vp.Perfil.Equals(profileName.ToUpper())) > 0;
            }
            return exists;
        }

        public static bool ProfileExists(int profileID, string profileName)
        {
            bool exists = false;
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                exists = ctx.Perfiles.Count(vp => vp.Perfil.Equals(profileName.ToUpper()) && vp.Id != profileID) > 0;
            }
            return exists;
        }

        public static IEnumerable<VincularPerfiles> GetProfiles()
        {
            IEnumerable<VincularPerfiles> foundProfiles = null;
            using (DCDocumentCenterDataContext dtctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foundProfiles = (from item in dtctx.Perfiles
                                 orderby item.Perfil descending
                                 select item);
            }
            return foundProfiles;
        }

        public static void DeleteProfile(int profileID)
        {
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                VincularPerfiles profileToDelete = ctx.Perfiles.SingleOrDefault(profile => profile.Id.Equals(profileID));
                if (profileToDelete != default(VincularPerfiles)) { ctx.Perfiles.RecycleOnSubmit(profileToDelete); }
                ctx.SubmitChanges();
            }
        }

        public static VincularPerfiles UpdateProfile(int profileID, string profileName, List<Roles> roleCollection, bool isSiteContributor)
        {
            VincularPerfiles elementToUpgrade = null;
            string oldProfileName = string.Empty;
            
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                elementToUpgrade = ctx.Perfiles.SingleOrDefault(profile => profile.Id.Equals(profileID));
                if (elementToUpgrade != default(VincularPerfiles)) {
                    oldProfileName = elementToUpgrade.Perfil;
                    elementToUpgrade.Perfil = profileName.ToUpper();
                    elementToUpgrade.ContribuyenteEnSitio = isSiteContributor;
                    elementToUpgrade.TipoRole.Clear();
                    roleCollection.ForEach(delegate(Roles role)
                    {
                        elementToUpgrade.TipoRole.Add(role);
                    });
                    ctx.SubmitChanges();                    
                }                
            }

            if (elementToUpgrade != null) { UpdateProfileProperties(oldProfileName, elementToUpgrade.Perfil); }

            return elementToUpgrade;
        }

        public static VincularPerfiles GetProfile(int profileID) {
            VincularPerfiles foundElement = null;
            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foundElement = ctx.Perfiles.SingleOrDefault(profile => profile.Id.Equals(profileID));
            }
            return foundElement;
        }

        private static void UpdateProfileProperties(string oldProfileName, string newProfileName) {
            Dictionary<string, string> users = PeopleHelper.GetUsersByProperty(GlobalConstants.UP_USER_PERFIL, oldProfileName, true);
            foreach (KeyValuePair<string, string> user in users) {
                PeopleHelper.UpdatePropertyUserProfile(GlobalConstants.UP_USER_PERFIL, newProfileName, user.Value);
            }
        }
    }
}
